﻿Imports System.Text

Public Class formMain

    Dim dtResource As DataTable = Nothing

    ''' <summary>
    ''' 窗体的Load函数
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub formMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        '选择连接类型下拉菜单
        Me.cmbConnType.Items.Add(DbfReadHelper.ConnType.MS_DBASE_DRIVER)
        Me.cmbConnType.Items.Add(DbfReadHelper.ConnType.MS_VFP_DRIVER)
        Me.cmbConnType.Items.Add(DbfReadHelper.ConnType.MS_VFP_OLEDB)
        Me.cmbConnType.Text = DbfReadHelper.ConnType.MS_VFP_OLEDB

        Me.txtSQL.Text = ""
        Me.txtSQL.Enabled = False

        Me.btnGenerate.Enabled = False



    End Sub

    ''' <summary>
    ''' 浏览文件，选取一个DBF文件
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnBrowse_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click

        Dim openFileDialog As OpenFileDialog = New OpenFileDialog With _
            {
                .AutoUpgradeEnabled = True, _
                .CheckFileExists = True, _
                .ReadOnlyChecked = False, _
                .Multiselect = False, _
                .FileName = "", _
                .Filter = "DBF文件|*.dbf", _
                .Title = "打开" _
            }

        If openFileDialog.ShowDialog() = DialogResult.OK Then

            Me.txtFilePath.Text = openFileDialog.FileName
            Me.cmbConnType.Text = "vfpoledb"
            Me.lsvColumnSelect.Items.Clear()
            Me.btnGenerate.Enabled = False

            Me.txtSQL.Text = "SELECT * FROM " & txtFilePath.Text
            Me.txtSQL.Enabled = True

        End If

    End Sub

    ''' <summary>
    ''' 测试该连接类型是否能够成功连接
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnTest_Click(sender As Object, e As EventArgs) Handles btnTest.Click

        If String.IsNullOrWhiteSpace(txtFilePath.Text) Then
            MessageBox.Show("请先选择一个DBF文件！")
            Return
        End If

        Try

            dtResource = DbfReadHelper.GetDataTable(txtFilePath.Text, txtSQL.Text, cmbConnType.Text)

            If (dtResource Is Nothing Or dtResource.Rows.Count = 0) Then
                Throw New Exception("数据库读取失败！")
            End If

        Catch ex As Exception
            Me.lsvColumnSelect.Items.Clear()
            Me.btnGenerate.Enabled = False
            MessageBox.Show(ex.Message)
            Return
        End Try

        lsvColumnSelect.Items.Clear()
        For i As Integer = 0 To dtResource.Columns.Count - 1
            lsvColumnSelect.Items.Add(dtResource.Columns(i).ColumnName)
        Next
        btnGenerate.Enabled = True

    End Sub

    Private Sub btnGenerate_Click(sender As Object, e As EventArgs) Handles btnGenerate.Click

        Dim saveFileDialog As SaveFileDialog = New SaveFileDialog() With _
            {
                .OverwritePrompt = True, _
                .Filter = "XML文件|*.xml" _
            }

        If saveFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then

            Try

                Dim sbSelected As StringBuilder = New StringBuilder()
                For i As Integer = 0 To lsvColumnSelect.Items.Count - 1
                    If lsvColumnSelect.Items(i).Checked Then
                        sbSelected.Append(lsvColumnSelect.Items(i).Text + "|")
                    End If
                Next

                Dim strColumns = sbSelected.ToString
                If String.IsNullOrWhiteSpace(strColumns) Then
                    MessageBox.Show("未选中任何信息")
                    Return
                Else
                    XmlWriteHelper.WriteDataTableToXml(txtFilePath.Text, saveFileDialog.FileName, dtResource, strColumns.Split("|".ToCharArray, StringSplitOptions.RemoveEmptyEntries))
                    MessageBox.Show("保存成功")
                End If

            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try

        End If

    End Sub

End Class
